package com.atguigu.myjuc2;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Number{
    private int number=60;
//    public synchronized void sale(){
//        if(number>0){
//            System.out.println(Thread.currentThread().getName()+"卖了第"+(number--)+"还剩"+number);
//            try {
//                TimeUnit.MILLISECONDS.sleep(200);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    }
    private final Lock lock=new ReentrantLock();
    public void sale(){
       lock.lock();
        try {
            if(number>0){
                System.out.println(Thread.currentThread().getName()+"卖出了第"+(number--)+"\t"+"还剩"+number);
            }
        } finally {
       lock.unlock();
        }
    }
}
public class SaleTicketDemo03 {
    public static void main(String[] args) {
        Number number = new Number();
        new Thread(()->{for (int i = 0; i < 60; i++) number.sale();},"张三").start();
        new Thread(()->{for (int i = 0; i < 60; i++) number.sale();},"李四").start();
        new Thread(()->{for (int i = 0; i < 60; i++) number.sale();},"王五").start();
    }
}
